package com.gd.iot.energy.service.systemadmin.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gd.iot.energy.code.ResponseCode;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.systemadmin.SupplierPOMapper;
import com.gd.iot.energy.exception.MyException;
import com.gd.iot.energy.po.systemadmin.SupplierPO;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.systemadmin.dto.SupplierSaveDTO;
import com.gd.iot.energy.pojo.systemadmin.vo.SupplierPageVO;
import com.gd.iot.energy.pojo.systemadmin.vo.SupplierVO;
import com.gd.iot.energy.service.systemadmin.SupplierService;
import com.gd.iot.energy.util.CustomBeanUtils;
import com.gd.iot.energy.util.CustomColumnUtil;
import com.gd.iot.energy.util.CustomStrUtil;
import com.gd.iot.energy.util.EnergyTypeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: zgyz
 * @Date: 2020/8/6 23:24
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierPOMapper, SupplierPO> implements SupplierService {

    @Resource
    private SupplierPOMapper supplierPOMapper;

    /**
     * 表头文件
     */
    private static final String SUPPLIER_LIST_COLUMN_FILE_NAME = "systemadmin/supplier_list.column";

    @Override
    public List<SupplierVO> getSupplierVOs(String supplierName) {
        QueryWrapper<SupplierPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (StrUtil.isNotBlank(supplierName)) {
            queryWrapper.like("supplier_name", supplierName);
        }
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        List<SupplierPO> supplierPOS = supplierPOMapper.selectList(queryWrapper);
        List<SupplierVO> supplierVOS = new ArrayList<>(supplierPOS.size());
        CustomBeanUtils.convertPojo(supplierPOS, supplierVOS, SupplierVO.class);
        return supplierVOS;
    }

    @Override
    public PageVO<SupplierPageVO> getPageSupplierVOs(String supplierName, String supplierContact, String supplierAddress,
                                                     Integer pageSize, Integer pageNum, String orderColumn, String orderType) {
        IPage<SupplierPO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SupplierPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (StrUtil.isNotBlank(supplierName)) {
            queryWrapper.like("supplier_name", supplierName);
        }
        if (StrUtil.isNotBlank(supplierContact)) {
            queryWrapper.like("supplier_contact", supplierContact);
        }
        if (StrUtil.isNotBlank(supplierAddress)) {
            queryWrapper.like("supplier_address", supplierAddress);
        }
        if (StrUtil.isNotBlank(orderColumn) && StrUtil.isNotBlank(orderType)) {
            orderColumn = CustomStrUtil.humpToLine(orderColumn);
            queryWrapper.orderBy(true, orderType.equals(SystemConsts.OrderType.ASC.value), orderColumn);
        }
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        IPage<SupplierPO> supplierPOIPage = supplierPOMapper.selectPage(page, queryWrapper);
        PageVO<SupplierPageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<SupplierPageVO> roleVOList = convertToSupplierPageVOList(supplierPOIPage.getRecords(), pageSize, pageNum);
        pageVO.setData(roleVOList);
        pageVO.setTotal(supplierPOIPage.getTotal());
        pageVO.setPages(supplierPOIPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(SUPPLIER_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    /**
     * 对象转换
     *
     * @param records
     * @return
     */
    private List<SupplierPageVO> convertToSupplierPageVOList(List<SupplierPO> records, Integer pageSize, Integer pageNo) {
        List<SupplierPageVO> res = new ArrayList<>(records.size());
        SupplierPageVO rolePageVO;
        int i = pageSize * (pageNo - 1) + 1;
        for (SupplierPO record : records) {
            rolePageVO = new SupplierPageVO();
            rolePageVO.setSerialNum(i);
            rolePageVO.setId(record.getId());
            rolePageVO.setSupplierSerialnumber(record.getSupplierSerialnumber());
            rolePageVO.setMemo(record.getMemo());
            rolePageVO.setSupplierAddress(record.getSupplierAddress());
            rolePageVO.setSupplierContact(record.getSupplierContact());
            rolePageVO.setSupplierEmail(record.getSupplierEmail());
            rolePageVO.setSupplierName(record.getSupplierName());
            rolePageVO.setSupplierPhone(record.getSupplierPhone());
            rolePageVO.setSupplierTel(record.getSupplierTel());
            res.add(rolePageVO);
            i++;
        }
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(SupplierSaveDTO supplierSaveDTO) {
        if (StrUtil.isBlank(supplierSaveDTO.getSupplierName()) ||
                StrUtil.isBlank(supplierSaveDTO.getSupplierContact()) ||
                StrUtil.isBlank(supplierSaveDTO.getSupplierAddress())) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        //邮箱合法性校验，可以为空，不为空格式需符合要求
        if (StrUtil.isNotBlank(supplierSaveDTO.getSupplierEmail()) &&
                !CustomStrUtil.isEmail(supplierSaveDTO.getSupplierEmail())) {
            throw new MyException(ResponseCode.EMAIL_FORMAT_ERROR);
        }

        SupplierPO supplierPO = new SupplierPO();
        supplierPO.setSupplierSerialnumber(supplierSaveDTO.getSupplierSerialnumber());
        supplierPO.setCreateDate(new Date());
        supplierPO.setModifyDate(new Date());
        supplierPO.setEnergyType(SystemConsts.EnergyTypeEnum.WATER.value);
        supplierPO.setIsDel(SystemConsts.BooleanEnum.FALSE.value);
        //todo 后期看下公司ID怎么取
        supplierPO.setCompanyId(0L);
        //todo 后期看下操作人ID怎么取
        supplierPO.setOperatorId(1L);
        supplierPO.setMemo(supplierSaveDTO.getMemo());
        supplierPO.setSupplierAddress(supplierSaveDTO.getSupplierAddress());
        supplierPO.setSupplierContact(supplierSaveDTO.getSupplierContact());
        supplierPO.setSupplierEmail(supplierSaveDTO.getSupplierEmail());
        supplierPO.setSupplierName(supplierSaveDTO.getSupplierName());
        supplierPO.setSupplierPhone(supplierSaveDTO.getSupplierPhone());
        supplierPO.setSupplierSerialnumber(supplierSaveDTO.getSupplierName());
        supplierPO.setSupplierTel(supplierSaveDTO.getSupplierTel());

        supplierPOMapper.insert(supplierPO);
        return supplierPO.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SupplierSaveDTO supplierSaveDTO) {
        if (StrUtil.isBlank(supplierSaveDTO.getSupplierName()) ||
                StrUtil.isBlank(supplierSaveDTO.getSupplierContact()) ||
                StrUtil.isBlank(supplierSaveDTO.getSupplierAddress())) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        //邮箱合法性校验，可以为空，不为空格式需符合要求
        if (StrUtil.isNotBlank(supplierSaveDTO.getSupplierEmail()) &&
                !CustomStrUtil.isEmail(supplierSaveDTO.getSupplierEmail())) {
            throw new MyException(ResponseCode.EMAIL_FORMAT_ERROR);
        }

        SupplierPO supplierPO = new SupplierPO();
        supplierPO.setId(supplierSaveDTO.getId());
        //todo 后期看下公司ID怎么取
        supplierPO.setCompanyId(0L);
        //todo 后期看下操作人ID怎么取
        supplierPO.setOperatorId(1L);
        supplierPO.setModifyDate(new Date());
        supplierPO.setMemo(supplierSaveDTO.getMemo());
        supplierPO.setSupplierAddress(supplierSaveDTO.getSupplierAddress());
        supplierPO.setSupplierContact(supplierSaveDTO.getSupplierContact());
        supplierPO.setSupplierEmail(supplierSaveDTO.getSupplierEmail());
        supplierPO.setSupplierName(supplierSaveDTO.getSupplierName());
        supplierPO.setSupplierPhone(supplierSaveDTO.getSupplierPhone());
        supplierPO.setSupplierSerialnumber(supplierSaveDTO.getSupplierSerialnumber());
        supplierPO.setSupplierTel(supplierSaveDTO.getSupplierTel());

        supplierPOMapper.updateById(supplierPO);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> ids) {
        if(CollUtil.isEmpty(ids)){
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        //供应商表逻辑删除
        QueryWrapper<SupplierPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        SupplierPO supplierPO = new SupplierPO();
        supplierPO.setIsDel(SystemConsts.BooleanEnum.TRUE.value);
        int num = supplierPOMapper.update(supplierPO,queryWrapper);

        return num > 0;
    }

    @Override
    public SupplierSaveDTO getSupplier(Integer id) {
        SupplierPO supplierPO = supplierPOMapper.selectById(id);
        if(supplierPO==null||SystemConsts.BooleanEnum.TRUE.value.equals(supplierPO.getIsDel())){
            throw new MyException(ResponseCode.RECORD_NOT_EXIST);
        }

        SupplierSaveDTO supplierSaveDTO = new SupplierSaveDTO();
        supplierSaveDTO.setId(supplierPO.getId());
        supplierSaveDTO.setSupplierSerialnumber(supplierPO.getSupplierSerialnumber());
        supplierSaveDTO.setMemo(supplierPO.getMemo());
        supplierSaveDTO.setSupplierAddress(supplierPO.getSupplierAddress());
        supplierSaveDTO.setSupplierContact(supplierPO.getSupplierContact());
        supplierSaveDTO.setSupplierEmail(supplierPO.getSupplierEmail());
        supplierSaveDTO.setSupplierName(supplierPO.getSupplierName());
        supplierSaveDTO.setSupplierPhone(supplierPO.getSupplierPhone());
        supplierSaveDTO.setSupplierTel(supplierPO.getSupplierTel());
        return supplierSaveDTO;
    }
}
